using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class UVOffsetManager : MonoBehaviour
{
	private static UVOffsetManager _instance;

	public Dictionary<string, UVOffsetList> PackDict;

	public List<UVOffsetList> UVOffsetTempList;

	public static UVOffsetManager Instance
	{
		get
		{
			if (_instance == null)
			{
				_instance = Object.FindObjectOfType<UVOffsetManager>();
				if (_instance == null)
				{
					GameObject gameObject = Resources.Load("UVChanger/ManagerUV") as GameObject;
					if (gameObject != null)
					{
						GameObject gameObject2 = Object.Instantiate(gameObject);
						gameObject2.name = gameObject.name;
						_instance = gameObject2.GetComponent<UVOffsetManager>();
					}
				}
			}
			return _instance;
		}
	}

	public void Start()
	{
		_instance = this;
		if (UVOffsetTempList != null && UVOffsetTempList.Count > 0)
		{
			if (PackDict == null || PackDict.Count == 0)
			{
				MakePackFromList();
			}
		}
		else
		{
			UVOffsetTempList = new List<UVOffsetList>();
			UVOffsetTempList.AddRange(GetComponentsInChildren<UVOffsetList>());
			MakePackFromList();
		}
	}

	public void MakePackFromList()
	{
		if (PackDict == null)
		{
			PackDict = new Dictionary<string, UVOffsetList>();
		}
		else
		{
			PackDict.Clear();
		}
		for (int i = 0; i < UVOffsetTempList.Count; i++)
		{
			PackDict.Add(UVOffsetTempList[i].gameObject.name, UVOffsetTempList[i]);
		}
	}
}
